const inquirer = require('inquirer')
const chalk = require('chalk')
const package = require('../package.json')
const log = console.log
const figlet = require('figlet')

figlet('codenalyse', function(err, data) {
  log(data)
  require('./checkVersion')(() => {
    log('')
    log(chalk.green('  ★ 欢迎使用sourceMap代码还原分析工具 ★'))
    log('')
    log(`${chalk.yellow('  版本号: ' + package.version)}`)
    log('')

    let validate = val => {
      if (/^\d+$/.test(val)) {
        return true
      }
      return '请输入数字'
    }

    const promptList = [
      {
        type: 'input',
        message: '请输入报错的行信息:',
        name: 'line',
        validate,
      },
      {
        type: 'input',
        message: '请输入报错的列信息:',
        name: 'column',
        validate,
      },
      {
        type: 'input',
        message: '请输入文件名字:',
        name: 'fileName',
        validate: val => {
          if (val.slice(-4) === '.map') {
            return true
          }
          return '文件名必须是.map结尾'
        },
      },
    ]

    inquirer.prompt(promptList).then(answers => {
      require('./sourceMap').sourceMap(answers)
    })
  })
})
